const path = require("path");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var webpack = require("webpack");
const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

var globalConfig = require("./globalConfig.json");

var baseModule = require("./baseModule.js");
var platformConfig = require("./platformConfig.js");

baseModule.mode = "production";
baseModule.devtool = "cheap-module-source-map";
delete baseModule.watchOptions;
baseModule.optimization = {
    splitChunks: {
        cacheGroups: {
            styles: {
                name: "styles",
                test: /\.css$/,
                chunks: "all",
                enforce: true
            }
        }
    },
    runtimeChunk: {
        name: "manifest"
    },
    minimizer: [new OptimizeCSSAssetsPlugin({})]
};

var config = baseModule;

function processModule(modules) {
    for (let x in modules) {
        let module = modules[x];
        module.plugins.splice(
            0,
            0,
            new UglifyJSPlugin({
                cache: true,
                parallel: true,
                sourceMap: true,
                uglifyOptions: {
                    compress: {
                        warnings: false
                    }
                }
            })
        );
        module.plugins.splice(
            2,
            0,
            new CleanWebpackPlugin(["dist"], {
                exclude: ["static", ".git"]
            }),
            new webpack.HashedModuleIdsPlugin()
        );
    }
}

processModule(platformConfig);

var appConfig = Object.assign({}, config, platformConfig.appConfig);
var omsConfig = Object.assign({}, config, platformConfig.omsConfig);
var pmConfig = Object.assign({}, config, platformConfig.pmConfig);
var spConfig = Object.assign({}, config, platformConfig.spConfig);
var hmsConfig = Object.assign({}, config, platformConfig.hmsConfig);
// Return Array of Configurations
module.exports = [appConfig, omsConfig, pmConfig, spConfig, hmsConfig];
